﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using System.Windows.Threading;
using Wpf_Examples.Tools;

namespace Wpf_Examples.Views
{
    /// <summary>
    /// ParticleCubeWaveWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ParticleCubeWaveWindow : Window
    {

        private readonly CreateParticleCubeWaveHelper particleCubeWaveHelper;
        private Point pMouse = new Point(9999, 9999);
        public ParticleCubeWaveWindow()
        {
            InitializeComponent();

            particleCubeWaveHelper = new CreateParticleCubeWaveHelper(30, 30, Colors.White);
           
            particleCubeWaveHelper.SpawnParticle(50);

            // 将粒子系统添加到场景中
            var modelVisual3D = new ModelVisual3D { Content = particleCubeWaveHelper.ParticleModel };
            viewport3D.Children.Add(modelVisual3D);
            // 绑定鼠标移动事件
            MouseMove += OnMouseMove;
        }
        /// <summary>
        /// 处理鼠标移动事件，更新粒子系统的高度。
        /// </summary>
        /// <param name="sender">事件发送者。</param>
        /// <param name="e">鼠标事件参数。</param>
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            var mousePosition = e.GetPosition(viewport3D);

            // 将屏幕坐标转换为视口内的三维坐标
            PointHitTestParameters pointparams = new PointHitTestParameters(mousePosition);
             VisualTreeHelper.HitTest(viewport3D, null, HTResult, pointparams);
            // 更新粒子系统的高度
            particleCubeWaveHelper.Update(pMouse);
        }

        /// <summary>
        /// 获取鼠标在场景中的3D坐标
        /// </summary>
        public HitTestResultBehavior HTResult(System.Windows.Media.HitTestResult rawresult)
        {
            RayHitTestResult rayResult = rawresult as RayHitTestResult;
            if (rayResult != null)
            {
                RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
                if (rayMeshResult != null)
                {
                    GeometryModel3D hitgeo = rayMeshResult.ModelHit as GeometryModel3D;
                    MeshGeometry3D hitmesh = hitgeo.Geometry as MeshGeometry3D;
                    Point3D p1 = hitmesh.Positions.ElementAt(rayMeshResult.VertexIndex1);
                    double weight1 = rayMeshResult.VertexWeight1;
                    Point3D p2 = hitmesh.Positions.ElementAt(rayMeshResult.VertexIndex2);
                    double weight2 = rayMeshResult.VertexWeight2;
                    Point3D p3 = hitmesh.Positions.ElementAt(rayMeshResult.VertexIndex3);
                    double weight3 = rayMeshResult.VertexWeight3;
                    Point3D prePoint = new Point3D(p1.X * weight1 + p2.X * weight2 + p3.X * weight3, p1.Y * weight1 + p2.Y * weight2 + p3.Y * weight3, p1.Z * weight1 + p2.Z * weight2 + p3.Z * weight3);
                    pMouse = new Point(prePoint.X, prePoint.Y);
                }
            }
            return HitTestResultBehavior.Continue;
        }
      
    }
}
